$(function () {
    // setNewsDetailsData();
    loadNewsDetails();
});

let i = parseInt(sessionStorage.getItem("newsi"));
let data = JSON.parse(sessionStorage.getItem("newsData"));

function loadNewsDetails() {
    setNewsType();
    setNewsDetailsData();
}

function setNewsType() {
    $(".news_type").html("");
    $.each(data.newsTypes, function (j, newstype) {
        if ((j + 1) == data.news[i].newsType.id)
            $(".news_type").append("<div class='currentNewsType' onclick='redirectNews(" + newstype.id + ")'>" + newstype.name + "</div>");
        else
            $(".news_type").append("<div onclick='redirectNews(" + newstype.id + ")'>" + newstype.name + "</div>");
    });
}
function redirectNews(id) {
    sessionStorage.setItem("news_type",id);
    window.location.href = "/html/front/news.html";
}

function setNewsDetailsData() {
    let last = i - 1;//设置json数据上一个索引值
    let next = i + 1;//设置json数据下一个索引值
    console.log(data);
    setData();
    pageJudge(last, next);
    $("#last_page").click(function () {
        if (last >= 0) {
            last--;
            i--;
            next--;
            setData();
            pageJudge(last, next);
        } else
            $("#last_page>span").html("无");
    });
    $("#next_page").click(function () {
        if (data.news[next] != undefined) {
            i++;
            last++;
            next++;
            setData();
            pageJudge(last, next);
        }
    });

}

function setData() {
    $("#news_title").html(data.news[i].title);
    $("#author").html(data.news[i].author);
    $("#create_time").html(data.news[i].create_time);
    $("#read_count").html(data.news[i].readCount);
    $("#news_intro").html(data.news[i].intro);
    $("#news_content").html(data.news[i].content);
}

function pageJudge(last, next) {
    if (last >= 0) //获取上一篇产品的产品名
        $("#last_page>span").html(data.news[last].title);
    else
        $("#last_page>span").html("无");

    if (data.news[next] != undefined) //获取下一篇产品的产品名
        $("#next_page>span").html(data.news[next].title);
    else
        $("#next_page>span").html("无");
}